package com.interview.com.entity;

import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by fangjicai on 2020/4/23.
 */
public class User {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        //java反射 <xml> <bean id="user" class="com.interview.com.entity.User" lazyInit=true/>
        //疑问？spring如何通过配置将类进行实例化
        //1.resourceLoader(资源加载器)加载xml文件，解析成资源信息，然后被xmlReader解析资源信息，转换成
        //BeanDefination对象（classname=com.interview.com.entity.User,classId=user,lazyInit=true）
        //2.将BeanDefination对象注册到注册表下（底层维护了一个CurrentHashMap，key是beanId,valueBeanDefination对象）,如何非懒加载则直接实例化
        //3.当上层应用用到UserBean，会去bean缓存池中查找，如果有，则直接返回，如果没有，需要从注册表中查询，通过
        //BeanDefination对象获取userbean的具体信息
        //4.利用java反射机制实例化bean，并存储缓存池！

        //java中获取类有三种方式
        //第一种
        Class c1=User.class;
        //第二种
        User user = new User();
        Class c2=user.getClass();

        //第三种
        Class<User> c3= null;
        try {
            c3= (Class<User>)Class.forName("com.interview.com.entity.User");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println(c1==c2);
        System.out.println(c2==c3);

        User user1=null;
        //利用反射获取实例对象,newInstance()必须具有无参构造才行
        try {
            //实例化对象
            user1 = c3.newInstance();
            user1.setName("fangjicai");
            user1.setAge(18);
            System.out.println(user1.toString());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        //bean缓存池
        ConcurrentHashMap<Object, Object> cacheContext = new ConcurrentHashMap<>();
        cacheContext.put("user",user1);
        cacheContext.get("user");



    }
}
